<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller\game;

/**
 * Description of GameController
 *
 * @author SoChishun
 */
class GameController extends \app\index\controller\BaseController {

    public function indexAction($type = '') {
        if ($this->loginInfo['currency'] < 1) {
            $this->error('您的竞猜币不足，请充值!', url('finance.recharge/recharge'));
        }
        $paychannels = ['index', 'wiipayh5', 'wiipaywx'];
        $rndchannel = ($type && in_array($type, $paychannels)) ? $type : $paychannels[0];
        return view($rndchannel);
    }

    public function doPostAction() {
        if ($this->loginInfo['currency'] < 1) {
            $this->error('您的竞猜币不足，请充值!', url('finance.recharge/recharge'));
        }
        $request = \think\Request::instance();
        $request->only(['da', 'xiao', 'dan', 'shuang']);
        $data = $request->post();
        $game = new \app\index\model\GameRecordModel();
        $result = $game->saveRecord($data, $this->loginInfo['id']);
        if (!$result['success']) {
            $this->error($result['message']);
        }
        redirect('notifyResult', 'out_trade_no=' . $result['message']['out_trade_no'])->send();
    }

    public function notifyResultAction() {
        $request = \think\Request::instance();
        $out_trade_no = $request->param('out_trade_no');
        if (!$out_trade_no) {
            exit('参数有误!');
        }
        $out_pay_no = date('YmdHis') . rand(1000, 9999);
        $game = new \app\index\model\GameRecordModel();
        $result = $game->notifyRecord($out_trade_no, $out_pay_no, $this->loginInfo['id']);
        if (!$result['success']) {
            $this->error($result['message'], url('index'));
        }
        redirect('result', 'out_trade_no=' . $out_trade_no)->send();
    }

    public function resultAction($out_trade_no = '') {
        if (!$out_trade_no) {
            exit('参数有误!');
        }
        $game = \app\index\model\GameRecordModel::get(['out_trade_no' => $out_trade_no]);
        if (null == $game) {
            exit('记录不存在');
        }
        $data = $game->toArray();
        return view('result', ['data' => $data]);
    }

    public function historyAction() {
        $where['user_id'] = $this->loginInfo['id'];
        $list = \app\index\model\GameRecordModel::where($where)->order('id desc')->paginate(10);
        return view('', ['list' => $list]);
    }

}
